
/**
 * 状态仓库
 *
 * Usage:
 *   import { Store } from './store'
 *   const store = new Store(options)
 */

import Vue from 'vue'
import Vuex from 'vuex'

import createVuexAlong from 'vuex-along'

Vue.use(Vuex)

export class Store extends Vuex.Store {
  constructor (options = {}) {
    options = Object.assign(options, {
      // 合并模块
      modules: Object.assign(getModules(), options.modules),
      // 合并插件
      plugins: Object.assign([
        // 持久化存储state插件
        createVuexAlong({
          // 仅使用sessionStorage
          justSession: true
        })
      ], options.plugins)
    })
    super(options)
  }
}

function getModules () {
  const files = require.context('./modules', false, /\.js$/)
  const modules = {}
  files.keys().forEach(key => {
    modules[key.replace(/(\.\/|\.js)/g, '')] = files(key).default
  })
  return modules
}
